#!/usr/bin/env php
<?php
// application.php

require __DIR__.'/vendor/autoload.php';

\app\entity\Config::$cwd = getcwd()."/";

class App {

    public static $argv ;
    public static $param = [];
    public static $option = [];

    public static function run($argv)
    {
        self::$argv = array_slice($argv , 1) ;
        self::parseArg();

        \app\entity\Config::$out =  new \app\entity\Output();
        self::exec();
    }

    public static function  getOption($key){
        if(isset(self::$option[$key])) {
            return self::$option[$key] ;
        }
        return null;
    }

    public static function parseArg(){
        $optionKey = "" ;
        foreach(self::$argv as $arg){
            if(strpos($arg , '-') !== false) {
                if($optionKey != ""){
                    self::$option[$optionKey] = true ;
                }
                $optionKey = substr($arg , 1);
            }else{
                if($optionKey == ""){
                    self::$param[] =  $arg ;
                }else{
                    self::$option[$optionKey] = $arg ;
                }
                $optionKey = '' ;
            }
        }

        if($optionKey != ""){
            self::$option[$optionKey] = true ;
        }
    }

    private static function exec()
    {
        $command = self::$param[0] ;
        $commandClass = "\\app\\command\\".ucfirst($command) . "Command" ;
        $object = new $commandClass();
        $object->execute();
    }
}

App::run($argv);